package tp.pr5.gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;

import tp.pr5.GameController;
import tp.pr5.MensajesConst;
import tp.pr5.PlayerObserver;
import tp.pr5.items.ItemList;

import java.awt.Cursor;

public class PlayerPanel extends JPanel implements PlayerObserver
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable _inventory;
	private String[] _cabeceras = new String[]
	{ "id", "Description" };

	@SuppressWarnings("unused")
	private GameController _game;

	private String[][] _elemToShow = null;

	private JLabel _lblhealthscore;

	public PlayerPanel(GameController game)
	{
		this._game = game;
		setBorder(new TitledBorder(null, "Player info", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		setLayout(new BorderLayout(0, 0));
//TODO
		_lblhealthscore =new JLabel("");// new JLabel(this._game.getPlayer().toString());
		_lblhealthscore.setHorizontalAlignment(SwingConstants.CENTER);
		add(_lblhealthscore, BorderLayout.NORTH);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		add(scrollPane, BorderLayout.CENTER);

		_inventory = new JTable();
		_inventory.setRequestFocusEnabled(false);
		_inventory.setModel(new DefaultTableModel(_elemToShow, this._cabeceras));
		_inventory.getColumnModel().getColumn(0).setResizable(false);
		_inventory.getColumnModel().getColumn(1).setResizable(false);
		_inventory.getColumnModel().getColumn(1).setPreferredWidth(423);
		scrollPane.setViewportView(_inventory);
	}

	

	public JTable get_inventory()
	{
		return this._inventory;
	}

	

	@Override
	public void itemEmpty(String itemName) {
		JOptionPane.showMessageDialog(null, "empty");
		
	}

	@Override
	public void itemLooked(String description) {
		JOptionPane.showMessageDialog(null, "looked");
		
	}

	@Override
	public void itemUsed(String itemName) {
		JOptionPane.showMessageDialog(null, MensajesConst.cambio);
		
	}

	@Override
	public void playerDead() {
		JOptionPane.showMessageDialog(null, "Al hoyo");
		
	}


	@Override
	public void playerUpdate(int newHealth, int newScore) {
		this._lblhealthscore.setText("Health : "+newHealth+", Score : "+newScore);	 
		
	}

	@Override public void playerLookedInventory(ItemList inventory)
	{
		JOptionPane.showMessageDialog(null, "oh! has mirado!");
		
	}

	@Override public void inventoryUpdate(ItemList inventory)
	{
		String lista=inventory.toString();
		String mensaje=MensajesConst.playerItems+lista;
				
		if (lista==null)
		{ 
			mensaje=MensajesConst.playerNoItems;
		}
		String items=mensaje;
		items=items.replaceAll("\n\n", "\n");items=items.replaceAll("\n\n", "\n");
		String[] itemList = items.split("\n");
		if (itemList.length < 1)
			return;
		int init;
		this._elemToShow = new String[itemList.length - 1][2];

		String desc;
		for (int i = 0; i < this._elemToShow.length; i++)
		{
			init = itemList[i + 1].indexOf('=') + 1;
			desc = itemList[i + 1].substring(init);
			String elem = itemList[i + 1].substring(0, init);
			elem = elem.substring(elem.indexOf('[') + 1, elem.indexOf(']'));
			this._elemToShow[i][0] = elem;
			this._elemToShow[i][1] = desc;
		}

		this._inventory.setModel(new DefaultTableModel(_elemToShow,
				this._cabeceras));
		
	}
}